package com.example.auth.controller;

import com.example.auth.service.ItripUserService;
import com.example.auth.service.SendCodeService;
import org.example.utils.AuthCode;
import org.example.utils.JWTUtil;
import org.example.utils.ResultTemplate;
import org.example.pojo.ItripUser;
import org.example.utils.SendMsgCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/itripUser")
@CrossOrigin
public class ItripUserController {
    @Autowired
    private SendCodeService sendCodeService;
    @Autowired
    private ItripUserService itripUserService;


    //获取验证码
    @GetMapping(value = "/getCode")
    public Object getCode(String phone,Integer page){
        if (sendCodeService.getPhoneCode(SendMsgCode.PageIsCode(page),phone))//调用发送验证码
            return new ResultTemplate(AuthCode.AUTH_CODE_GET_SUCCESS);
        else
            return new ResultTemplate(AuthCode.AUTH_CODE_GET_ERROR);
    }


    //注册验证手机号和验证码是否存在
    @PostMapping(value = "/checkRegisterCode")
    public Object checkRegisterCode(String phone,String code,Integer page){
        if (sendCodeService.existsCode(SendMsgCode.PageIsCode(page),code)){//判断验证码是否正确
            if (itripUserService.existsUser(phone)==null){
                return new ResultTemplate(AuthCode.AUTH_CODE_SUCCESS);
            }else{
                return new ResultTemplate(AuthCode.AUTH_REGISTER_ERROR_EXISTS_USER);
            }
        }else{
            return new ResultTemplate(AuthCode.AUTH_ERROR_CODE);
        }
    }


    //注册
    @PostMapping(value = "/register")
    public Object register(@ModelAttribute ItripUser itripUser){
        int result = itripUserService.insertUser(itripUser);
        AuthCode authCode = null;
        if (result!=0){
            authCode = AuthCode.AUTH_REGISTER_SUCCESS;
        }else{
            authCode = AuthCode.AUTH_REGISTER_ERROR;
        }
        return new ResultTemplate(authCode);
    }

    //登录
    @PostMapping(value = "/login")
    public Object login(@RequestParam String phone,
                                @RequestParam(name = "code",required = false) String code,
                                @RequestParam(name = "password",required = false) String password,
                                @RequestParam(required = false) Integer page){
        Map<String,Object> map = new HashMap<>();
        AuthCode authCode = null;
        ItripUser itripUser = itripUserService.existsUser(phone);//验证手机号是否存在
        if (itripUser!=null){
            map.put("user",itripUser);
            if (password!=null && !password.equals("")){//判断是否为密码登录
                if (itripUserService.loginByPwd(phone, password)){//判断密码是否正确
                    map.put("token", JWTUtil.create("phone",phone));
                    authCode = AuthCode.AUTH_LOGIN_SUCCESS;
                }else{
                    authCode = AuthCode.AUTH_LOGIN_ERROR_PWD;
                }
            }
            if (code!=null && !code.equals("")){//判断是否为验证码登录
                if (sendCodeService.existsCode(SendMsgCode.PageIsCode(page),code)){//判断验证码是否正确
                    map.put("token", JWTUtil.create("phone",phone));
                    authCode = AuthCode.AUTH_LOGIN_SUCCESS;
                }else{
                    authCode = AuthCode.AUTH_ERROR_CODE;
                }
            }
        }else{
            authCode = AuthCode.AUTH_LOGIN_ERROR_NOT_USER;
        }
        return new ResultTemplate(authCode, map);
    }
}
